﻿//using System;
//using System.Linq;
//using System.Threading.Tasks;
//using Microsoft.AspNetCore.Identity;
//using Microsoft.AspNetCore.Http;
//using Microsoft.EntityFrameworkCore;
//using SX.Module.Core.Models; 
//using SX.Entity;
//using SmartSql.DyRepository;
//namespace SX.Module.Core.Extensions
//{
//    public class WorkContext : IWorkContext
//    {
//        private const string UserGuidCookiesName = "SimplUserGuid";
//        private const long GuestRoleId = 3;

//        private Users _currentUser;
//        private UserManager<Users> _userManager;
//        private HttpContext _httpContext;
//        private IRepository<Users,long> _userRepository;

//        public WorkContext(UserManager<Users> userManager, IHttpContextAccessor contextAccessor, IRepository<Users, long> userRepository)
//        {
//            _userManager = userManager;
//            _httpContext = contextAccessor.HttpContext;
//            _userRepository = userRepository;
//        }

//        public async Task<Users> GetCurrentUser()
//        {
//            if (_currentUser != null)
//            {
//                return _currentUser;
//            }

//            var contextUser = _httpContext.User;
//            _currentUser = await _userManager.GetUserAsync(contextUser);

//            if (_currentUser != null)
//            {
//                return _currentUser;
//            }

//            var userGuid = GetUserGuidFromCookies();
//            if (userGuid.HasValue)
//            {
//                _currentUser = _userRepository.Query().Include(x => x.Roles).FirstOrDefault(x => x.UserGuid == userGuid);
//            }

//            if (_currentUser != null && _currentUser.Roles.Count == 1 && _currentUser.Roles.First().RoleId == GuestRoleId)
//            {
//                return _currentUser;
//            }

//            userGuid = Guid.NewGuid();
//            var dummyEmail = string.Format("{0}@guest.simplcommerce.com", userGuid);
//            _currentUser = new User
//            {
//                FullName = "Guest",
//                UserGuid = userGuid.Value,
//                Email = dummyEmail,
//                UserName = dummyEmail
//            };
//            var abc = await _userManager.CreateAsync(_currentUser, "1qazZAQ!");
//            await _userManager.AddToRoleAsync(_currentUser, "guest");
//            SetUserGuidCookies(_currentUser.UserGuid);
//            return _currentUser;
//        }

//        private Guid? GetUserGuidFromCookies()
//        {
//            if (_httpContext.Request.Cookies.ContainsKey(UserGuidCookiesName))
//            {
//                return Guid.Parse(_httpContext.Request.Cookies[UserGuidCookiesName]);
//            }

//            return null;
//        }

//        private void SetUserGuidCookies(Guid userGuid)
//        {
//            _httpContext.Response.Cookies.Append(UserGuidCookiesName, _currentUser.UserGuid.ToString(), new CookieOptions
//            {
//                Expires = DateTime.UtcNow.AddYears(5),
//                HttpOnly = true,
//                IsEssential = true
//            });
//        }
//    }
//}
